-- 还没学到模块，先记住这个固定格式吧
module Main
    where

import IO		-- 导入IO库
import Random		-- 导入随机数库

-- 这个函数用于判断一个元素是否存在与一个列表中，典型的haskell式递归函数
my_inList p [] = False		
my_inList p (x:xs) = 
  if p == x
    then True
    else my_inList p xs

-- 这个函数用于判断一个字符串表示的是否是非负整数，典型的haskell式递归函数。可以用map + isDigit代替。
my_isInt [] = False
my_isInt [x] = 
  if my_inList x ['0','1','2','3','4','5','6','7','8','9']
    then True
    else False
my_isInt (x:xs) = 
  if my_inList x ['0','1','2','3','4','5','6','7','8','9']
    then my_isInt xs
    else False

-- 提示用户输入幸运数字，幸运数字必须介于1到36之间，否则会重复提示用户输入正确的数字
askForLuckyNum = do
  putStrLn "Please enter you lucky number:"
  line <- getLine
  if my_isInt line
    then do
      let num = read line
      if my_inList num [1..36]
        then return num
        else do
	  putStrLn "what you have input is not in the range 1 .. 36!"
          askForLuckyNum
    else do
      putStrLn "must input a number!"
      askForLuckyNum

-- 生成36选7的7个数。
genList x =  
  if length x >= 7
    then return x
    else do
      num <- randomRIO (1::Int, 36)
      if my_inList num x
        then genList x
        else genList $ num:x

-- 主程序入口，获取用户输入的幸运数字，并打印出随机产生的7个数。
main = do 
  num <- askForLuckyNum
  result <- genList [num]
  print result
  putStrLn "press enter to continue ...."
  line <- getLine
  putStrLn "bye"

